/* eslint-disable max-len */
/**
 * yq address
 * url: http://localhost:3002/m1/yq/address
 */
import { httpFetch } from '../../utils/req'
import sha256 from 'crypto-js/sha256'
import ceh from 'crypto-js/enc-hex'

const e = (new Date().getTime() / 1e3).toFixed()
const i = '23y0ufFl5YxIyGrI8hWRUZmKkvtSjLQA'
const a = '123456789abcdefg'
const s = 'zdww'

function getSignature1 () {
  return sha256(e + i + a + e).toString(ceh).toUpperCase()
}

function getSignature2 () {
  return sha256(
    e + 'fTN2pfuisxTavbTuYVSsNJHetwq5bJvCQkjjtiLM2dCratiA' + e
  ).toString(ceh).toUpperCase()
}

const s1 = getSignature1()
const s2 = getSignature2()

const dict = {
  appId: 'NcApplication',
  key: '3C502C97ABDA40D0A60FBEE50FAAD1DA',
  nonceHeader: '123456789abcdefg',
  paasHeader: 'zdww',
  signatureHeader: s1,
  timestampHeader: e
}

const headers = {
  Referer: 'http://bmfw.www.gov.cn/',
  Origin: 'http://bmfw.www.gov.cn',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0',
  'x-wif-nonce': 'QkjjtiLM2dCratiA',
  'x-wif-paasid': 'smt-application',
  'x-wif-signature': s2,
  'x-wif-timestamp': e,
  'Content-Type': 'application/json; charset=utf-8'
}

const url = 'http://bmfw.www.gov.cn//bjww/interface/interfaceJson'

export default (query, request) => {
  return new Promise((resolve, reject) => {
    httpFetch(
      url,
      {
        method: 'post',
        body: dict,
        headers: headers
      }
    ).promise.then(res => {
      resolve(res.body)
    }).catch(e => {
      reject(e)
    })
  })
}
